package com.sunshine.schedulegrid.utils

import android.annotation.SuppressLint
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author sunshine
 * @date 2020/9/1
 * 日期处理类
 */
internal object DateUtils {
    //创建日历对象
    private val calendar = Calendar.getInstance()

    //获取今年
    private val year = calendar[Calendar.YEAR]

    //获取本月。加1为实际月份
    private val month = calendar[Calendar.MONTH] + 1

    //默认开学时间
    private val startDate: String = "$year-${getStartMonth()}-1"

    //一周长度的long值
    const val aWeekLong: Long = 1000 * 60 * 60 * 24 * 7

    //时间规范
    @SuppressLint("SimpleDateFormat")
    private val simpleDateFormat: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd")

    /**
     * 获取开学月份
     * 当当前月份小于8，默认为3月开学
     * 否则默认为9月开学
     */
    private fun getStartMonth(): Int{
        return if (month<8) 3 else 9
    }

    //返回默认开学时间的long值
    fun getStartDateLong(): Long{
        return simpleDateFormat.parse(startDate)!!.time
    }

    //返回指定时间的long值
    fun getDateLong(dateString: String): Long{
        return simpleDateFormat.parse(dateString)!!.time
    }

    //返回现在时间的long值
    fun getNowDateLong(): Long {
        return calendar.timeInMillis
    }

    //返回当前为第几周
    fun getThisWeek(thisDate: Long): Int {
        return ((thisDate - getStartDateLong()) / aWeekLong + 1).toInt()
    }
    fun getThisWeek(thisDate: Date): Int {
        return ((thisDate.time - getStartDateLong()) / aWeekLong + 1).toInt()
    }
    //返回今天为第几周
    fun getThisWeek(): Int {
        return ((getNowDateLong() - getStartDateLong()) / aWeekLong + 1).toInt()
    }

    //返回以周一开始的今天是一周的周几
    fun getDayOfWeek(day: Int): Int {
        return if (day - 1 == 0) 7
        else day - 1
    }
}